package com.ruoyi.web.controller.net;

import com.ruoyi.net.domain.*;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.IElementOspfv3Service;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.service.IElementOspfv3DomainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元-OSPFV3进程配置-域配置Controller
 * 
 * @author net
 * @date 2024-11-17
 */
@Api(tags = "网元-OSPFV3进程配置-域配置")
@RestController
@RequestMapping("/element/ospfv3/domain")
public class ElementOspfv3DomainController extends BaseController
{
    @Autowired
    private IElementOspfv3DomainService elementOspfv3DomainService;

    @Autowired
    private IElementOspfv3Service elementOspfv3Service;
    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询网元-OSPFV3进程配置-域配置列表
     */
    @ApiOperation(value = "查询网元-OSPFV3进程配置-域配置列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:domain:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElementOspfv3Domain elementOspfv3Domain)
    {
        startPage();
        List<ElementOspfv3Domain> list = elementOspfv3DomainService.selectElementOspfv3DomainList(elementOspfv3Domain);
        return getDataTable(list);
    }

    /**
     * 导出网元-OSPFV3进程配置-域配置列表
     */
    @ApiOperation(value = "导出网元-OSPFV3进程配置-域配置列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:domain:export')")
    @Log(title = "网元-OSPFV3进程配置-域配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElementOspfv3Domain elementOspfv3Domain)
    {
        List<ElementOspfv3Domain> list = elementOspfv3DomainService.selectElementOspfv3DomainList(elementOspfv3Domain);
        ExcelUtil<ElementOspfv3Domain> util = new ExcelUtil<ElementOspfv3Domain>(ElementOspfv3Domain.class);
        util.exportExcel(response, list, "网元-OSPFV3进程配置-域配置数据");
    }

    /**
     * 获取网元-OSPFV3进程配置-域配置详细信息
     */
    @ApiOperation(value = "获取网元-OSPFV3进程配置-域配置详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:domain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(elementOspfv3DomainService.selectElementOspfv3DomainById(id));
    }

    /**
     * 新增网元-OSPFV3进程配置-域配置
     */
    @ApiOperation(value = "新增网元-OSPFV3进程配置-域配置", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:domain:add')")
    @Log(title = "网元-OSPFV3进程配置-域配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElementOspfv3Domain elementOspfv3Domain)
    {
        sendXml(elementOspfv3Domain);
        return toAjax(elementOspfv3DomainService.insertElementOspfv3Domain(elementOspfv3Domain));
    }

    /**
     * 修改网元-OSPFV3进程配置-域配置
     */
    @ApiOperation(value = "修改网元-OSPFV3进程配置-域配置", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('system:domain:edit')")
    @Log(title = "网元-OSPFV3进程配置-域配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElementOspfv3Domain elementOspfv3Domain)
    {
        sendXml(elementOspfv3Domain);
        return toAjax(elementOspfv3DomainService.updateElementOspfv3Domain(elementOspfv3Domain));
    }

    /**
     * 删除网元-OSPFV3进程配置-域配置
     */
    @ApiOperation(value = "删除网元-OSPFV3进程配置-域配置", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('system:domain:remove')")
    @Log(title = "网元-OSPFV3进程配置-域配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(elementOspfv3DomainService.deleteElementOspfv3DomainByIds(ids));
    }


    private void sendXml(ElementOspfv3Domain entity) {
        // TODO: 2024/12/3  设备联调，添加并激活
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getElementId());
            deviceManager.getDevice(netElement.getIp());

            ElementOspfv3 elementOspfv3 = elementOspfv3Service.selectElementOspfv3ById(entity.getOspfv3Id());

            String operation = "create";
            if ("0".equals(entity.getStatus())) {
                operation = "remove";
            }
            StringBuilder xml = new StringBuilder();
            xml.append("<ospfv3 xmlns=\"http://fiberhome.com/fhnw/yang/ipprotocol/routing/fos-ospfv3\">" +
                    "    <ospfv3-processes>" +
                    "      <ospfv3-process nc:operation=\"" + operation + "\">" +
                    "        <process-id>" + elementOspfv3.getOspfId() + "</process-id>" +
                    //---- start 域配置 ---
                    "        <area>" +
                    "          <area-id>" + entity.getDomainId() + "</area-id>" +
                    "          <area-type>" + entity.getDomainType() + "</area-type>" +
                    "          <stub-no-summary>" + entity.getStubEnable() + "</stub-no-summary>" +
                    "          <ipsec-policy>" + entity.getIpsec() + "</ipsec-policy>" +
                    "           <nssa>"+
//                    "               <translator-role>"+entity.getNssaConfig().split("|")[0]+"</translator-role>"+
//                    "               <default-enable>"+entity.getNssaConfig().split("|")[1]+"</default-enable>"+
//                    "               <type>"+entity.getNssaConfig().split("|")[2]+"</type>"+
//                    "               <cost>"+entity.getNssaConfig().split("|")[3]+"</cost>"+
//                    "               <no-summary>"+entity.getNssaConfig().split("|")[4]+"</no-summary>"+
                    "           </nssa>"+
                    "           <virtual-link>"+
//                    "               <neighbor-id>"+entity.getVirtualConfig().split("|")[0]+"</neighbor-id>"+
//                    "               <dead-interval>"+entity.getVirtualConfig().split("|")[1]+"</dead-interval>"+
//                    "               <retransmit-interval>"+entity.getVirtualConfig().split("|")[2]+"</retransmit-interval>"+
//                    "               <transmit-delay-interval>"+entity.getVirtualConfig().split("|")[3]+"</transmit-delay-interval>"+
//                    "               <hello-interval>"+entity.getVirtualConfig().split("|")[4]+"</hello-interval>"+
                    "           </virtual-link>"+
                   /* "           <filter>"+
                    "               <filter-name>"+entity.getFilterConfig().split("|")[0]+"</filter-name>"+
                    "               <filter-type>"+entity.getFilterConfig().split("|")[1]+"</filter-type>"+
                    "           </filter>"+*/
                    "           <abr-summary>"+
//                    "               <summary-prefix>"+entity.getConvergeRoute().split("|")[0]+"</summary-prefix>"+
//                    "               <not-advertise>"+entity.getConvergeRoute().split("|")[1]+"</not-advertise>"+
                    "           </abr-summary>"+
                    "        </area>" +
                    //---- end 域配置 ----
                    "      </ospfv3-process>" +
                    "    </ospfv3-processes>" +
                    "  </ospfv3>");
            System.out.println(xml.toString());
            deviceManager.editConfig(xml.toString(), "merge");
        }
    }
}
